package com.boxman;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;

import java.io.InputStream;
import java.util.Locale;

// 箱子
class Box extends BaseGameObject {
    private BaseGameObject mBack;   // 背后对象
    protected Bitmap mCurrentImage; // 当前图片
    protected Bitmap mImage2;       // 图片2

    /**
     * 构造方法。
     * @param assetManager  资产管理者
     * @param back          背后对象
     * @param drawer        绘图者
     * @param type          类型
     * @param x, y          左上角坐标
     */
    public Box(AssetManager assetManager, BaseGameObject back, Canvas drawer, Type type, int x, int y) {
        super(assetManager, drawer, type, x, y);

        // 初始化成员
        mBack = back;
        mCurrentImage = mImage;
        try {
            final String imageFilePath = String.format(Locale.getDefault(), Const.IMAGE_FILE_PATH, type.ordinal() * 10 + 1);
            final InputStream inputStream = assetManager.open(imageFilePath);
            mImage2 = BitmapFactory.decodeStream(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 移动。
     * @param direction     方向
     * @param moveResult    移动结果
     * @return 被推动的箱子
     */
    @Override
    public BaseGameObject move(int direction, MoveResult moveResult) {
        boolean leftFromDest = false;   // 是否离开目的地
        int destX = mX, destY = mY;     // 新位置的坐标

        // 计算新位置
        if (direction == R.id.IDC_BUTTON_LEFT) {
            destX--;
        } else if (direction == R.id.IDC_BUTTON_RIGHT) {
            destX++;
        } else if (direction == R.id.IDC_BUTTON_UP) {
            destY--;
        } else if (direction == R.id.IDC_BUTTON_DOWN) {
            destY++;
        }

        // 如果旁边是墙或箱子，则直接返回
        if (Game.sBaseGameObjects[destY][destX].getType() == Type.WALL ||
            Game.sBaseGameObjects[destY][destX].getType() == Type.BOX) {
            moveResult.result = MoveResult.Result.NO_MOVE;
            return null;
        }

        // 设置移动结果
        if (moveResult.result.ordinal() < MoveResult.Result.NORMAL_MOVED.ordinal()) {
            moveResult.result = MoveResult.Result.NORMAL_MOVED;
        }

        // 判断是否要离开目的地
        if (mType == Type.BOX && mBack.getType() == Type.DESTINATION) {
            leftFromDest = true;
            moveResult.result = MoveResult.Result.LEFT_FROM_DEST;
            mCurrentImage = mImage;
        }

        // 用背后对象擦除自己
        mBack.draw();
        Game.sBaseGameObjects[mY][mX] = mBack;

        // 计算新位置
        mX = destX;
        mY = destY;

        // 设置新的背后对象
        mBack = Game.sBaseGameObjects[mY][mX];

        // 判断是否到达目的地
        if (mType == Type.BOX && mBack.getType() == Type.DESTINATION) {
            if (leftFromDest) {
                moveResult.result = MoveResult.Result.NORMAL_MOVED;
            } else {
                moveResult.result = MoveResult.Result.ARRIVED_ON_DEST;
            }
            mCurrentImage = mImage2;
        }

        // 重绘自己
        final Rect srcRect = new Rect(0, 0, mCurrentImage.getWidth(), mCurrentImage.getHeight());
        final Rect dstRect = new Rect(mX * sSize, mY * sSize, (mX + 1) * sSize, (mY + 1) * sSize);
        mDrawer.drawBitmap(mCurrentImage, srcRect, dstRect, null);

        // 将自己保存到数组中
        Game.sBaseGameObjects[mY][mX] = this;

        return this;
    }
}
